home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Miscellaneous / Randy Thelen / ThreadedSort / Sort ƒ / SortPictsThreads.cp < prev   
Encoding:
Text File  |  1994-06-26  |  1.8 KB  |  114 lines  |  [TEXT/MMCC]

  1. #include "SortPicts.h"
  2.  
  3.  
  4. void    SortPicts::MakeThreaded( void)
  5. {
  6.     ThreadID                tempThreadInfo;
  7.     OSErr                    errWhatErr;
  8.     
  9.     randomSeed = TickCount();
  10.     
  11.     unitCount = newUnitCount = 1;
  12.     
  13.     startTime = TickCount();
  14.  
  15.     errWhatErr = NewThread( kCooperativeThread, 
  16.                             SortPictsThreadEntry, 
  17.                             (void *)this, 
  18.                             100000,
  19.                             kCreateIfNeeded,
  20.                             (void**)nil,
  21.                             &tempThreadInfo);
  22.     
  23.     if( errWhatErr)
  24.         DebugStr( "\pXcuzzU?  NewThread Failed??");
  25.     
  26.     threadInfo = tempThreadInfo;
  27.     
  28.     YieldToThread( threadInfo);
  29.  
  30. }
  31.  
  32.  
  33. pascal    void    *SortPictsThreadEntry( void *there)
  34. {
  35.     SortPicts        *t = (SortPicts *)there;
  36.  
  37.     t->Entry();
  38.     
  39.     return nil;
  40. }
  41.  
  42.  
  43. void    SortPicts::Entry( void)
  44. {
  45.     while( 1)
  46.     {
  47.         Scramble();
  48.         Update();
  49.         Sort();
  50.     }
  51. }
  52.  
  53.  
  54. /*
  55.  *    SetSortItem
  56.  *
  57.  */
  58. void    SortPicts::SetSortItem( long index, long data)
  59. {
  60.     long                horiz, vert;
  61.     long                offset;
  62.     
  63.     horiz = index % pictWidth;
  64.     vert = index / pictWidth;
  65.     
  66.     if( vert >= copyBitsRect.bottom)
  67.         copyBitsRect.bottom = vert+1;
  68.  
  69.     if( vert < copyBitsRect.top)
  70.         copyBitsRect.top = vert;
  71.  
  72.     sortData[index] = data;
  73.     offset = horiz + (vert * pictRowBytes);
  74.     sortPixels[offset] = (SortPixel) data;
  75.     
  76.     if( !unitCount--)
  77.         Yield();
  78. }
  79.  
  80. void    SortPicts::ExchangeSortItem( long one, long theOther)
  81. {
  82.     long        temp;
  83.     
  84.     temp = sortData[one];
  85.     SetSortItem( one, sortData[theOther]);
  86.     SetSortItem( theOther, temp);
  87. }
  88.  
  89.  
  90. void    SortPicts::Yield( void)
  91. {
  92.     CalculateNewUnitCount();
  93.     Update();
  94.     UnuseSortData();
  95.     YieldToAnyThread();
  96.     startTime = TickCount();
  97.     UseSortData();
  98. }
  99.  
  100. void    SortPicts::CalculateNewUnitCount( void)
  101. {
  102.     long            deltaTime = TickCount() - startTime;
  103.     
  104.     if( deltaTime)
  105.         newUnitCount = (int) (((_Float) newUnitCount) * 
  106. //                    ((_Float) kYieldTime / (_Float) deltaTime));
  107.                     ((_Float) 6 / (_Float) deltaTime));
  108.     else
  109.         newUnitCount = newUnitCount * 6 + 1;
  110.     
  111.     unitCount = newUnitCount;
  112. }
  113.  
  114.